<?php
/**
 * Plugin Name: Floating Widgets
 * Plugin URI: http://floating-widgets.affilisites.com
 * Description: Adds a dynamic widget area to begining of post or page
 * Version: 1.03
 * Author: Affilisites
 * Author URI: http://floating-widgets.affilisites.com
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
 




add_filter( "the_content", "inject_floating_widget" );
add_action ("init", "init_floating_widgets");
add_action('admin_menu', 'floating_widgets_menu');
register_activation_hook( __FILE__, 'floating_widgets_activate');
add_action('wp_footer', 'floating_widgets_footer_link');

function floating_widgets_footer_link() {

if (get_option('floating_widgets_appreciation')) echo '<a href="http://floating-widgets.affilisites.com">Floating Widgets</a>';

}

function floating_widgets_activate() {
update_option ('floating_widgets_appreciation', 'on');
update_option ('floating_widgets_mode', 'basic');
}


function init_floating_widgets() {

if ( function_exists('register_sidebar') )

if (get_option('floating_widgets_mode')=="advanced") {
register_sidebar(array(
'name' => 'Floating Widget',
'before_widget' => get_option('floating_widgets_before_widget'),
'after_widget' => get_option('floating_widgets_after_widget'),
'before_title' => get_option('floating_widgets_before_title'),
'after_title' => get_option('floating_widgets_after_title')
));
}

if (get_option('floating_widgets_mode')=="basic")  {
	if (get_option('floating_widgets_title_size')) $h2_style  = $h2_style  . "font-size:" . get_option('floating_widgets_title_size') . "px;";
	if (get_option('floating_widgets_title_color')) $h2_style  = $h2_style  . "color:#" . get_option('floating_widgets_title_color') . ";";
	if ($h2_style) $h2_style = ' style="' . $h2_style . '"';

register_sidebar(array(
'name' => 'Floating Widget',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2' . $h2_style . '>',
'after_title' => '</h2>'
));
}


}

function inject_floating_widget($content) {
	global $post;
	ob_start();
	if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Floating Widget") ) : 
	endif;
	$wool = ob_get_clean();
	if (get_post_type($post)=="post") {
		if (get_option('floating_widgets_posts')) {
			if (get_option('floating_widgets_mode')=="advanced") $content = get_option('floating_widgets_before_section') . $wool . get_option('floating_widgets_after_section') . get_option('floating_widgets_before_content') . $content . get_option('floating_widgets_after_content');  
			if (get_option('floating_widgets_mode')=="basic") {
				if (get_option('floating_widget_float')!=="none") $style = $style . "float:" . get_option('floating_widget_float') . ";";
				if (get_option('floating_widget_width')) $style  = $style  . "width:" . get_option('floating_widget_width') . "px;";
				if (get_option('floating_widget_padding')) $style  = $style  . "padding:" . get_option('floating_widget_padding') . "px;";
				if (get_option('floating_widget_margin')) $style  = $style  . "margin:" . get_option('floating_widget_margin') . "px;";
				if (get_option('floating_widget_border_width')) $style  = $style  . "border:" . get_option('floating_widget_border_width') . "px " . get_option('floating_widget_border_style') . " #" . get_option('floating_widget_border_color') . ";";
				if ($style) $style = ' style="' . $style . '"';
				$content = "<div" . $style . ">" . $wool . "</div>" . $content; 
			}
		}
	}
	if (get_post_type($post)=="page") {
		if (get_option('floating_widgets_pages')) {
			if (get_option('floating_widgets_mode')=="advanced") $content = get_option('floating_widgets_before_section') . $wool . get_option('floating_widgets_after_section') . get_option('floating_widgets_before_content') . $content . get_option('floating_widgets_after_content');  
			if (get_option('floating_widgets_mode')=="basic") {
				if (get_option('floating_widget_float')!=="none") $style = $style . "float:" . get_option('floating_widget_float') . ";";
				if (get_option('floating_widget_width')) $style  = $style  . "width:" . get_option('floating_widget_width') . "px;";
				if (get_option('floating_widget_padding')) $style  = $style  . "padding:" . get_option('floating_widget_padding') . "px;";
				if (get_option('floating_widget_margin')) $style  = $style  . "margin:" . get_option('floating_widget_margin') . "px;";
				if (get_option('floating_widget_border_width')) $style  = $style  . "border:" . get_option('floating_widget_border_width') . "px " . get_option('floating_widget_border_style') . " #" . get_option('floating_widget_border_color') . ";";
				if ($style) $style = ' style="' . $style . '"';
				$content = "<div" . $style . ">" . $wool . "</div>" . $content; 
			}
		}
	}
	if (get_option('floating_widgets_custom_' . get_post_type($post))) {
		if (get_option('floating_widgets_mode')=="advanced") $content = get_option('floating_widgets_before_section') . $wool . get_option('floating_widgets_after_section') . get_option('floating_widgets_before_content') . $content . get_option('floating_widgets_after_content');  
		if (get_option('floating_widgets_mode')=="basic") {
			if (get_option('floating_widget_float')!=="none") $style = $style . "float:" . get_option('floating_widget_float') . ";";
			if (get_option('floating_widget_width')) $style  = $style  . "width:" . get_option('floating_widget_width') . "px;";
			if (get_option('floating_widget_padding')) $style  = $style  . "padding:" . get_option('floating_widget_padding') . "px;";
			if (get_option('floating_widget_margin')) $style  = $style  . "margin:" . get_option('floating_widget_margin') . "px;";
			if (get_option('floating_widget_border_width')) $style  = $style  . "border:" . get_option('floating_widget_border_width') . "px " . get_option('floating_widget_border_style') . " #" . get_option('floating_widget_border_color') . ";";
			if ($style) $style = ' style="' . $style . '"';
			$content = "<div" . $style . ">" . $wool . "</div>" . $content; 
		}
	}	
	return $content;
}




function floating_widgets_menu() {
	add_theme_page('Floating Widgets', 'Floating Widgets', '8', 'floating_widgets', 'floating_widgets_page');




}




function floating_widgets_page() {
	if (get_option('floating_widgets_mode')=="basic") floating_widgets_basic(); else floating_widgets_advanced();
	

}


function floating_widgets_advanced() {
	?>

	<div class='wrap'>
	

	
	<div id="icon-themes" class="icon32"></div>
	<h2>Floating Widget Settings (Advanced Mode)</h2> 
	
	<form method="post" action="options.php">
		<?php wp_nonce_field('update-options') ?>
		<input type="hidden" name="floating_widgets_mode" value="basic">
		<input class="button-secondary" type="submit" name="Submit" value="Switch to Basic Mode" />
		<input type="hidden" name="action" value="update" />
		<input type="hidden" name="page_options" value="floating_widgets_mode" />
	</form>	

	<form method="post" action="options.php" style="padding-top:30px;">
	<?php wp_nonce_field('update-options') ?>
	<h3 class="title">General Settings</h3>
	<table class="form-table">
		<tbody>
			<tr valign="top">
				<th scope="row">
					<label for="floating_widgets_posts">Display on:</label>
				</th>
				<td>
					<input type="checkbox" <?php  checked(  get_option('floating_widgets_posts'), on); ?> id="floating_widgets_posts" name="floating_widgets_posts"><label for="floating_widgets_posts">Posts</label><br/>
					<input type="checkbox" <?php  checked(  get_option('floating_widgets_pages'), on); ?> id="floating_widgets_pages" name="floating_widgets_pages"><label for="floating_widgets_pages">Pages</label>
<?php 
	$args=array(
		'public'   => true,
		'_builtin' => false
	); 
	$output = 'names'; // names or objects
	$operator = 'and'; // 'and' or 'or'
	$post_types=get_post_types($args,$output,$operator); 
	foreach ($post_types  as $post_type ) : 
		$custom_post_types_ids = $custom_post_types_ids . ",floating_widgets_custom_" . $post_type;
?>
					<br/><input type="checkbox" <?php  checked(  get_option('floating_widgets_custom_'. $post_type), on); ?> id="floating_widgets_custom_<?php echo $post_type;?>" name="floating_widgets_custom_<?php echo $post_type;?>"><label for="floating_widgets_custom_<?php echo $post_type;?>"><?php echo ucwords($post_type);?></label>
<?php endforeach; ?>
				</td>
			</tr>
		</tbody>
	</table>
	<h3 class="title">Widget Section</h3>
	<p style="width:90%;">This is the code that surrounds the entire widget section. When widgetizing a theme the traditional way, this is the code that would go before and after  the dynamic_sidebar() function.</p>
	<table class="form-table">
		<tbody>
			<tr valign="top">
				<th scope="row">
					<label for="floating_widgets_before_section">Before Section:</label>
				</th>
				<td>
					
					<textarea  name="floating_widgets_before_section" id="floating_widgets_before_section" cols="100%" rows="3"><?php echo get_option('floating_widgets_before_section'); ?></textarea>
				</td>
			</tr>
			<tr valign="top">
				<th scope="row">
					<label for="floating_widgets_after_section">After Section:</label>
				</th>
				<td>
					<textarea  name="floating_widgets_after_section"  id="floating_widgets_after_section" cols="100%" rows="3"><?php echo get_option('floating_widgets_after_section'); ?></textarea>
				</td>
			</tr>
		</tbody>
	</table>
	<h3 class="title">Individual Widgets</h3>
	<p style="width:90%;">This is the code that surrounds each individual widget. When widgetizing a theme the traditional way, this is the code that would go in the 'before_widget', 'after_widget', 'before_title' and 'after_title' of the register_sidebar() function.</p>
	<table class="form-table">
		<tbody>
			<tr valign="top">
				<th scope="row">
					<label for="floating_widgets_before_widget">Before Widget:</label>
				</th>
				<td>
					<textarea  name="floating_widgets_before_widget" id="floating_widgets_before_widget" cols="100%" rows="3"><?php echo get_option('floating_widgets_before_widget'); ?></textarea>
				</td>
			</tr>
			<tr valign="top">
				<th scope="row">
					<label for="floating_widgets_after_widget">After Widget:</label>
				</th>
				<td>
					<textarea  name="floating_widgets_after_widget" id="floating_widgets_after_widget" cols="100%" rows="3"><?php echo get_option('floating_widgets_after_widget'); ?></textarea>
				</td>
			</tr>
			<tr valign="top">
				<th scope="row">
					<label for="floating_widgets_before_title">Title:</label>
				</th>
				<td>
					<label for="floating_widgets_before_title">Before:</label> <input  name="floating_widgets_before_title" id="floating_widgets_before_title" value="<?php echo get_option('floating_widgets_before_title'); ?>" />
					<label for="floating_widgets_after_title">After:</label> <input  name="floating_widgets_after_title" id="floating_widgets_after_title" value="<?php echo get_option('floating_widgets_after_title'); ?>" />
				</td>
			</tr>

		</tbody>
	</table>
	<h3 class="title">Post Content</h3>
	<p style="width:90%;">Since we are injecting the widget area right before the the_content() function of your theme, there may be need to encase your content with some extra styling.</p>
	<table class="form-table">
		<tbody>
			<tr valign="top">
				<th scope="row">
					<label for="floating_widgets_before_content">Before Content:</label>
				</th>
				<td>
					<textarea  name="floating_widgets_before_content"  id="floating_widgets_before_content" cols="100%" rows="3"><?php echo get_option('floating_widgets_before_content'); ?></textarea>
				</td>
			</tr>
			<tr valign="top">
				<th scope="row">
					<label for="floating_widgets_after_content">After Content:</label>
				</th>
				<td>
					<textarea  name="floating_widgets_after_content"  id="floating_widgets_after_content" cols="100%" rows="3"><?php echo get_option('floating_widgets_after_content'); ?></textarea>
				</td>
			</tr>			</tbody>
	</table>
	<h3 class="title">Footer Link</h3>
	<p style="width:90%;">Show your appreciation for this plugin by allowing me to place a small text link in your footer.</p>
	<table class="form-table">
		<tbody>
			<tr valign="top">
				<th scope="row">
					<label for="floating_widgets_posts">Display link:</label>
				</th>
				<td>
					<input type="checkbox" <?php  checked(  get_option('floating_widgets_appreciation'), on); ?> id="floating_widgets_appreciation" name="floating_widgets_appreciation">
				</td>
			</tr>
		</tbody>
	</table>
	<p class="submit"><input class="button-primary" type="submit" name="Submit" value="Update Options" /></p>

	<input type="hidden" name="action" value="update" />
	<input type="hidden" name="page_options" value="floating_widgets_posts,floating_widgets_pages<?php echo $custom_post_types_ids;?>,floating_widgets_before_section,floating_widgets_after_section,floating_widgets_before_widget,floating_widgets_after_widget,floating_widgets_before_title,floating_widgets_after_title,floating_widgets_before_content,floating_widgets_after_content,floating_widgets_appreciation" />
	</form>
	
	
	
	
	</div>
	<?php
}


function floating_widgets_basic() {
	?>

	<div class='wrap'>
	

	
	<div id="icon-themes" class="icon32"></div>
	<h2>Floating Widget Settings (Basic Mode)</h2> 
	

	<form method="post" action="options.php">
		<?php wp_nonce_field('update-options') ?>
		<input type="hidden" name="floating_widgets_mode" value="advanced">
		<input class="button-secondary" type="submit" name="Submit" value="Switch to Advanced Mode" />
		<input type="hidden" name="action" value="update" />
		<input type="hidden" name="page_options" value="floating_widgets_mode" />
	</form>

	
	<form method="post" action="options.php">
	<?php wp_nonce_field('update-options') ?>
	<h3 class="title">General Settings</h3>
	<table class="form-table">
		<tbody>
			<tr valign="top">
				<th scope="row">
					<label for="floating_widgets_posts">Display on:</label>
				</th>
				<td>
					<input type="checkbox" <?php  checked(  get_option('floating_widgets_posts'), on); ?> id="floating_widgets_posts" name="floating_widgets_posts"><label for="floating_widgets_posts">Posts</label><br/>
					<input type="checkbox" <?php  checked(  get_option('floating_widgets_pages'), on); ?> id="floating_widgets_pages" name="floating_widgets_pages"><label for="floating_widgets_pages">Pages</label>
<?php 
	$args=array(
		'public'   => true,
		'_builtin' => false
	); 
	$output = 'names'; // names or objects
	$operator = 'and'; // 'and' or 'or'
	$post_types=get_post_types($args,$output,$operator); 
	foreach ($post_types  as $post_type ) : 
		$custom_post_types_ids = $custom_post_types_ids . ",floating_widgets_custom_" . $post_type;
?>
					<br/><input type="checkbox" <?php  checked(  get_option('floating_widgets_custom_'. $post_type), on); ?> id="floating_widgets_custom_<?php echo $post_type;?>" name="floating_widgets_custom_<?php echo $post_type;?>"><label for="floating_widgets_custom_<?php echo $post_type;?>"><?php echo ucwords($post_type);?></label>
<?php endforeach; ?>
				</td>
			</tr>
		</tbody>
	</table>	
	<h3 class="title">Style Settings</h3>
	<table class="form-table">
		<tbody>
			<tr valign="top">
				<th scope="row">
					<label for="floating_widget_width">Width:</label>
				</th>
				<td>
					<input type="text" size="4" name="floating_widget_width" id="floating_widget_width" value="<?php echo get_option('floating_widget_width');?>" /><label for="floating_widget_width">px</label>
				</td>
			</tr>
			<tr valign="top">
				<th scope="row">
					<label for="floating_widget_float">Alignment:</label>
				</th>
				<td>
					<select name="floating_widget_float">
						<option value="right" <?php if (get_option('floating_widget_float')=='right') echo 'Selected'?>>Right</option>
						<option value="left"  <?php if (get_option('floating_widget_float')=='left') echo 'Selected'?>>Left</option>
					</select>
				</td>
			</tr>			
			<tr valign="top">
				<th scope="row">
					<label for="floating_widget_border_width">Border:</label>
				</th>
				<td>
					<input type="text" size="1" name="floating_widget_border_width" id="floating_widget_border_width" value="<?php echo get_option('floating_widget_border_width');?>" /><label for="floating_widget_border_width">px</label>
					<select name="floating_widget_border_style">
						<option value="dotted" <?php if (get_option('floating_widget_border_style')=='dotted') echo 'Selected'?>>Dotted</option>
						<option value="dashed"  <?php if (get_option('floating_widget_border_style')=='dashed') echo 'Selected'?>>Dashed</option>
						<option value="solid" <?php if (get_option('floating_widget_border_style')=='solid') echo 'Selected'?>>Solid</option>
						<option value="double"  <?php if (get_option('floating_widget_border_style')=='double') echo 'Selected'?>>Double</option>
						<option value="groove" <?php if (get_option('floating_widget_border_style')=='groove') echo 'Selected'?>>Groove</option>
						<option value="ridge"  <?php if (get_option('floating_widget_border_style')=='ridge') echo 'Selected'?>>Ridge</option>
						<option value="inset" <?php if (get_option('floating_widget_border_style')=='inset') echo 'Selected'?>>Inset</option>
						<option value="outset"  <?php if (get_option('floating_widget_border_style')=='outset') echo 'Selected'?>>Outset</option>
					</select>
					<label for="floating_widget_border_color">Color: #</label><input type="text" size="6" name="floating_widget_border_color" id="floating_widget_border_color" value="<?php echo get_option('floating_widget_border_color');?>" />
				</td>
			</tr>
			<tr valign="top">
				<th scope="row">
					<label for="floating_widget_padding">Padding:</label>
				</th>
				<td>
					<input type="text" size="4" name="floating_widget_padding" id="floating_widget_padding" value="<?php echo get_option('floating_widget_padding');?>" /><label for="floating_widget_padding">px</label>
				</td>
			</tr>			
			<tr valign="top">
				<th scope="row">
					<label for="floating_widget_margin">Margin:</label>
				</th>
				<td>
					<input type="text" size="4" name="floating_widget_margin" id="floating_widget_margin" value="<?php echo get_option('floating_widget_margin');?>" /><label for="floating_widget_margin">px</label>
				</td>
			</tr>	
			<tr valign="top">
				<th scope="row">
					<label for="floating_widgets_title_size">Title:</label>
				</th>
				<td>
					<label for="floating_widgets_title_size">Font Size:</label><input type="text" size="1" name="floating_widgets_title_size" id="floating_widgets_title_size" value="<?php echo get_option('floating_widgets_title_size');?>" /><label for="floating_widgets_title_size">px</label><br/>
					<label for="floating_widgets_title_color">Color: #</label><input type="text" size="6" name="floating_widgets_title_color" id="floating_widgets_title_color" value="<?php echo get_option('floating_widgets_title_color');?>" />
				</td>
			</tr>			
		</tbody>
	</table>	
	<h3 class="title">Footer Link</h3>
	<p style="width:90%;">Show your appreciation for this plugin by allowing me to place a small text link in your footer.</p>
	<table class="form-table">
		<tbody>
			<tr valign="top">
				<th scope="row">
					<label for="floating_widgets_posts">Display link:</label>
				</th>
				<td>
					<input type="checkbox" <?php  checked(  get_option('floating_widgets_appreciation'), on); ?> id="floating_widgets_appreciation" name="floating_widgets_appreciation">
				</td>
			</tr>
		</tbody>
	</table>
	<p class="submit"><input class="button-primary" type="submit" name="Submit" value="Update Options" /></p>

	<input type="hidden" name="action" value="update" />
	<input type="hidden" name="page_options" value="floating_widgets_posts,floating_widgets_pages<?php echo $custom_post_types_ids;?>,floating_widget_width,floating_widget_float,floating_widget_border_width,floating_widget_border_style,floating_widget_border_color,floating_widget_padding,floating_widget_margin,floating_widgets_title_size,floating_widgets_title_color,floating_widgets_appreciation" />

	</form>
	</div>
	<?php
}

?>